﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsOrderManagement
{
    //订单服务：添加、删除、修改、查询（所有、ID、名称、客户、订单金额）
    public class OrderService
    {
        public List<Order> orders = new List<Order>();

        // 添加订单
        public void AddOrder(Order order)
        {
    
            orders.Add(order);
        }

        // 删除订单
        public void RemoveOrder(string orderId)
        {
            var order = orders.FirstOrDefault(o => o.OrderId == orderId);
            orders.Remove(order);
        }
        // 修改订单
        public void ModifyOrder(Order modifiedOrder)
        {
            var existingOrder = orders.FirstOrDefault(o => o.OrderId == modifiedOrder.OrderId);
            orders.Remove(existingOrder);
            orders.Add(modifiedOrder);
        }

        // 查询所有订单
        public List<Order> GetAllOrders()
        {
            return orders.OrderBy(o => o.TotalAmount).ToList();
        }

        // 按订单ID查询
        public List<Order> QueryByOrderId(string orderId)
        {
            var query = orders
                .Where(o => o.OrderId.Contains(orderId))
                .OrderBy(o => o.TotalAmount)
                .ToList();

      
            return query;
        }

        // 按商品名称查询
        public List<Order> QueryByProductName(string productName)
        {
            var query = orders
                .Where(o => o.OrderDetails!=null&&o.OrderDetails.Product.NAME.Contains(productName))
                .OrderBy(o => o.TotalAmount)
                .ToList();

            return query;
        }

        // 按客户查询
        public List<Order> QueryByCustomer(string customerName)
        {
            var query = orders
                .Where(o => o.Customer.Name.Contains(customerName))
                .OrderBy(o => o.TotalAmount)
                .ToList();
            return query;
        }

        // 按订单金额范围查询
        public List<Order> QueryByAmountRange(decimal minAmount, decimal maxAmount)
        {
            var query = orders
                .Where(o => o.TotalAmount >= minAmount && o.TotalAmount <= maxAmount)
                .OrderBy(o => o.TotalAmount)
                .ToList();
            return query;
        }

        // 排序订单
        public void SortOrders(Comparison<Order> comparison = null)
        {
            if (comparison == null)
            {
                orders.Sort(); // 使用默认的IComparable实现
            }
            else
            {
                orders.Sort(comparison);
            }
        }

        // 导出订单到文件
        public void ExportToFile(string filePath)
        {
            try
            {
                System.IO.File.WriteAllText(filePath, string.Join("\n\n", orders.Select(o => o.ToString())));
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to export orders: {ex.Message}");
            }
        }

        // 从文件导入订单
        public void ImportFromFile(string filePath)
        {
            throw new NotImplementedException("Import functionality not implemented in this example.");
        }

    }
}
